﻿using UnityEngine;
using UnityEngine.SceneManagement;
using System.Diagnostics;
using UnityEngine.UI;
using System.Collections;
using System.Xml;
using System.Collections.Generic;

public class squareScene : MonoBehaviour
{
    /// <summary>
    /// This script should be set for cardboard camera.
    /// Buttons, sphere of movie can be controled.
    /// </summary>
    MediaPlayerCtrl m_Media;
    int movieID = 0;
    bool isPlay = true;
    Stopwatch time = new Stopwatch();
    public int playTime;// The time of movie
    public string quitScene;// Quit for next scene
    public string libraryScene = "libraryScene";
    //public string tBuilding5Scene = "tBuilding5Scene";

    // Use this for initialization
    void Start()
    {
        // Get m_Media of playing movies
        m_Media = GameObject.FindGameObjectWithTag("mSphere").GetComponent<MediaPlayerCtrl>();

        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("mSphere"))
        {
            obj.GetComponent<Transform>().position = new Vector3(0, 0, 0);
            time.Start();
        }
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("pSphere"))
        {
            obj.GetComponent<Transform>().position = new Vector3(0, -100, 0);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (time.ElapsedMilliseconds >= playTime * 1000 && isPlay)
        {
            isPlay = false;
            time.Stop();
            MovieChangeToPhoto();
        }
    }

    private void MovieChangeToPhoto()
    {
        // Change the position of sphere
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("pSphere"))
        {
            obj.GetComponent<Transform>().position = new Vector3(0, 0, 0);
        }
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("mSphere"))
        {
            obj.GetComponent<Transform>().position = new Vector3(0, -100, 0);
        }
    }

    //加载视频
    public void Load()
    {
        time.Reset();
        time.Start();
        m_Media.Load(m_Media.m_strFileName);
    }

    //播放视频
    public void Play()
    {
        time.Start();
        m_Media.Play();
    }

    //暂停视频
    public void Pause()
    {
        time.Stop();
        m_Media.Pause();
    }

    // Quit this scene
    public void Quit()
    {
        SceneManager.LoadScene(quitScene);
    }

    // Go to the library scene
    public void LibraryScene()
    {
        SceneManager.LoadScene(libraryScene);
    }
}
